using System;
using System.Collections.Generic;
using System.Text;
using Ricciolo.PaperBoy.Feeds;
using System.ComponentModel;

namespace Ricciolo.PaperBoy.UI
{
    public class FeedSettings : INotifyPropertyChanged
    {
        private string _name;
        private string _address;
        private FeedInterval _syncSettings;
        private int _interval;
        private bool _downloadEnclosuresAutomatically;
        private int _maxItemCount;
        private bool _isKeepAllItems;
        private Feed feed;
        private FeedsManager manager;

        private PropertyChangedEventHandler _propertyChanged;

        private static Interval[] intervals = new Interval[] { new Interval(15, "15 minutes"), new Interval(30, "30 minutes"), new Interval(60, "1 hour"), new Interval(240, "4 hours"), new Interval(60 * 24, "1 day"), new Interval(60 * 24 * 7, "1 week"), new Interval(-1, "never") };

        public FeedSettings(FeedsManager manager, Feed feed)
        {
            if (manager == null)
                throw new ArgumentNullException("manager");
            if (feed == null)
                throw new ArgumentNullException("feed");
            this.feed = feed;
            this.manager = manager;

            this._name = feed.Name;
            this._address = feed.Url.ToString();
            this._syncSettings = feed.SyncSetting;
            this._interval = feed.Interval;
            this._downloadEnclosuresAutomatically = feed.DownloadEnclosuresAutomatically;
            this._maxItemCount = feed.MaxItemCount;
            this._isKeepAllItems = (this.MaxItemCount == 0);
        }

        public void Commit()
        {
            feed.Name = this.Name;
            feed.SyncSetting = this.SyncSettings;
            feed.Interval = (this.Interval > 0) ? this.Interval : 15;
            if (this.Interval < 0)
                feed.SyncSetting = FeedInterval.Manually;
            feed.DownloadEnclosuresAutomatically = this.DownloadEnclosuresAutomatically;
            feed.MaxItemCount = (this.IsKeepAllItems) ? 0 : this.MaxItemCount;
        }

        public Interval[] Intervals
        {
            get { return intervals; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Address
        {
            get { return _address; }
        }

        public FeedInterval SyncSettings
        {
            get { return _syncSettings; }
            set
            {
                if (_syncSettings != value)
                {
                    _syncSettings = value;
                    OnPropertyChanged("SyncSettings");
                }
            }
        }

        public int Interval
        {
            get { return _interval; }
            set
            {
                if (_interval != value)
                {
                    _interval = value;
                    OnPropertyChanged("Interval");
                }
            }
        }

        public string DefaultInterval
        {
            get
            {
                int i = this.manager.DefaultInterval;
                TimeSpan ts = TimeSpan.FromMinutes(i);
                // Format value
                if (ts.Ticks >= TimeSpan.TicksPerDay)
                    return ts.TotalDays.ToString() + " days";
                else if (ts.Ticks >= TimeSpan.TicksPerHour)
                    return ts.TotalHours.ToString() + " hours";
                else
                    return ts.TotalMinutes.ToString() + " minutes";
            }
        }

        public bool DownloadEnclosuresAutomatically
        {
            get { return _downloadEnclosuresAutomatically; }
            set { _downloadEnclosuresAutomatically = value; }
        }

        public int MaxItemCount
        {
            get { return _maxItemCount; }
            set { _maxItemCount = value; }
        }

        public int ItemCountLimit
        {
            get { return this.manager.ItemCountLimit; }
        }

        public bool IsCustomSync
        {
            get { return this.SyncSettings != FeedInterval.Default; }
            set
            {
                this.SyncSettings = FeedInterval.Interval;
                OnPropertyChanged("IsCustomSync");
                OnPropertyChanged("IsDefaultSync");
            }
        }

        public bool IsDefaultSync
        {
            get { return this.SyncSettings == FeedInterval.Default; }
            set
            {
                this.SyncSettings = FeedInterval.Default;
                OnPropertyChanged("IsCustomSync");
                OnPropertyChanged("IsDefaultSync");
            }
        }

        public bool IsKeepAllItems
        {
            get { return _isKeepAllItems; }
            set
            {
                _isKeepAllItems = value;
                OnPropertyChanged("IsKeepAllItems");
                OnPropertyChanged("IsKeepCustomItems");
            }
        }

        public bool IsKeepCustomItems
        {
            get { return !_isKeepAllItems; }
            set
            {
                _isKeepAllItems = !value;
                OnPropertyChanged("IsKeepAllItems");
                OnPropertyChanged("IsKeepCustomItems");
            }
        }

        #region INotifyPropertyChanged Members

        event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
        {
            add { _propertyChanged += value; }
            remove { _propertyChanged -= value; }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (_propertyChanged != null)
                _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }

    public class Interval
    {
        private int _value;
        private string _description;

        public Interval(int value, string description)
        {
            _value = value;
            _description = description;
        }

        public int Value
        {
            get { return _value; }
        }

        public string Description
        {
            get { return _description; }
        }
    }
}
